/******************************************************************************
*
*   Filename:   mw_schm.c 
*
*   Created_by: 
*       
*   Date_created: 
*
*   Description:  Main Scheduler file
*
*
*******************************************************************************
*   Revision History:
*
*   Date          CQ#           Author    
*   MM-DD-YY      oem00000000   Initials   Description of change.
*   -----------   -----------   --------   ------------------------------------
******************************************************************************/

/******************************************************************************
*   Include Files
******************************************************************************/ 
#include    "mw_schm.h" 
#include    "mw_schm_trm_isr.h" 
#include    "mw_schm_cfg.h" 
#include    "hw_timers.h" 
#include    "sw_timers.h" 
#include    "interrupts.h"


/******************************************************************************
*   Local Macro Definitions
******************************************************************************/ 

/******************************************************************************
*   Local Type Definitions
******************************************************************************/

/******************************************************************************
*   Local Function Declarations
******************************************************************************/ 

/******************************************************************************
*   Global Variable Definitions
******************************************************************************/
    T_UWORD  curOpMode;

/******************************************************************************
*   Static Variable Definitions
******************************************************************************/

/******************************************************************************
*   Global and Static Function Definitions
******************************************************************************/ 



/***************************************************************************
 * Function		: mw_schm_schInit
 * Description	: Main function of Scheduler module
 * Returns		: void
 * Notes		:
 **************************************************************************/ 
void mw_schm_schInit()
{
    MODULES_ID_TYPE mngIndex;

	mw_interrupt_initCfg();

    /* clear timer interrupt flag */
    mngExecflag = FALSE;

    /* Disable Interrupt */
    mw_interrupt_Disable_Interrupts();

    /* Call modules Init function, configured in mw_schm_cfg.c */
    for (mngIndex = 0; mngIndex < SCHD_MANAGERS_NUMBER; mngIndex++)
    { 
        if (mngTaskList[mngIndex].ManagerInitAPI != SCHM_NULL_PTR)
        {
            mngTaskList[mngIndex].ManagerInitAPI();    
        }
    }

    /***********************************************************/
    /* Init timer */
    timer0_initCfg();
    /***********************************************************/

          
    /* Enable Interrupt */
    mw_interrupt_Enable_Interrupts();
    
    /* Initialize software timers */
    sw_timers_InitTimers();

    curOpMode = 0x01;

}

/***************************************************************************
 * Function		: mw_schm_schExec
 * Description	: Main function of Scheduler module
 * Returns		: void
 * Notes		:
 **************************************************************************/ 
void mw_schm_schExec(void)
{
    MODULES_ID_TYPE mngIndex;
    T_SWORD MngTimeCnt[SCHD_MANAGERS_NUMBER];    
    T_UWORD measStartTime = 0;
 

    /* Initialization of local modules counters. Delays module(Task) execution from Scheduler start */
    for (mngIndex = 0; mngIndex < SCHD_MANAGERS_NUMBER; mngIndex++)
    {
        MngTimeCnt[mngIndex] = (T_SWORD)mngTaskList[mngIndex].StartUpDelay;    
    }  
    
    do
    {
        /* wait for timer interrupt */
        if (mngExecflag)
        {
             /* clear timer interrupt flag */
            mngExecflag = FALSE;
            /* check each module from the list of modules (TASKS) */
            for (mngIndex = 0; mngIndex < SCHD_MANAGERS_NUMBER; mngIndex++)
            {
                /*Check if there is a valid Task*/
				if (mngTaskList[mngIndex].ManagerAPI != SCHM_NULL_PTR)
                {
                    /* decrement the local counter of modules  */
                    MngTimeCnt[mngIndex] = MngTimeCnt[mngIndex] - TIMER_PERIOD;    
                    /* check whether counter is expired */
					/*the first time since the variable is a signed word after substraction variable is 0xFFFF this is -1*/
                    if (MngTimeCnt[mngIndex] <= 0)
                    {
                        /* set the local module counter */
                        MngTimeCnt[mngIndex] = (T_SWORD)mngTaskList[mngIndex].ExecutionPeriod;
                        /* check whether the module starts for the current Mode */
                        if (0 != (mngTaskList[mngIndex].UsedInMode & curOpMode))
                        {
                            /* start module if counter is expired */
                            measStartTime = 0;
                            /* Start measurement */
                            //SCHD_measStart(&measStartTime);
                            mngTaskList[mngIndex].ManagerAPI();    
                            //SCHD_measFinish(&measStartTime, mngIndex);
                        }
                    }
                }
            }  
        }
    }
    while (TRUE);

    return;
}
